import mitt from 'mitt'

class Message{
    constructor(){
        this.event = mitt();
        this.init(); //初始化
    }
    init(){
        global.addEventListener('message',(e)=>{
            const msg=e.data; //获取消息
            const {type,body}=msg;
            this.event.emit(type,body); //触发事件
        });
    }

    receive(type,callback){
        this.event.on(type,callback);
    }

    send(msg){
        global.postMessage(msg)
    }
}

export default new Message();